package team.kirohuji.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import team.kirohuji.domain.user.Priority;
import team.kirohuji.domain.user.Role;
import team.kirohuji.repository.jpa.user.PriorityRepository;
import team.kirohuji.repository.jpa.user.RoleRepository;
import team.kirohuji.service.user.PriorityService;
import team.kirohuji.service.user.RoleService;
import team.kirohuji.utils.R;

@Controller
public class RoleController {

	@Resource
	private RoleService roleService;
	@Resource
	private PriorityRepository priorityRepository;
	@Resource
	private PriorityService priorityService;
	@Autowired
	private RoleRepository roleRepository;

	// 列出所有角色
	@ResponseBody
	@RequestMapping(value = "/listAllRoles")
	public R listAllRoles() {

		List<Role> roles = roleRepository.findAll();

		return R.ok().put("roles", roles);

	}

	// 分页
	@ResponseBody
	@RequestMapping(value = "/lists")
	public Page<Role> limitListRoles(@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		Sort sort = new Sort(Direction.DESC, "RoleId");
		Pageable pageable = new PageRequest(page, size, sort);
		return roleRepository.findAll(pageable);
	}
	// 列出所有父权限 update by ywm 09.24
	@ResponseBody
	@RequestMapping(value = "/listParentPriority")
	public String listParentPriority() {

		List<Priority> priorities = priorityRepository.queryParentPriority();

		return JSON.toJSONString(priorities);

	}

	// 添加角色
	@ResponseBody
	@RequestMapping(value = "/addRole")
	public Map<String, Object> addRole(@ModelAttribute Role role) {

		Role flag = roleRepository.save(role);
		Map<String, Object> map = new HashMap<>();

		if (flag != null) {
			map.put("message", "success");
			map.put("role", role);
		} else {
			map.put("message", "该角色名已存在");
		}

		return map;

	}

	// 修改角色权限 update by ywm 09.25
	@ResponseBody
	@RequestMapping(value = "/modifyRolePriority")
	public Map<String, Object> modifyRolePriority(@ModelAttribute Role role) {
		return null;

	}

	/*
	 * //删除角色
	 * 
	 * @ResponseBody
	 * 
	 * @RequestMapping(value = "/deleteRole") public Map<String, Object>
	 * deleteRole(String roleid) { Map<String,Object> map = new HashMap<>(); Integer
	 * result = systemRoleService.deleteRole(Integer.parseInt(roleid)); if(result ==
	 * 0){ map.put("message", "failed"); return map; } map.put("message",
	 * "success"); return map; }
	 */

}
